import * as roadService from '../../services/design/roadServices';
import { notification } from 'antd';
export default {

    namespace: 'refRoads',
    state: {
        refRoadlines: [],
        currentRoad: null,
        roads: [],
        offset: null,
        bridgeCenterOffset: null,
        maxY: null

    },
    reducers: {
        setInitPoint(state, { payload: offset }) {
            return { ...state, offset };
        },
        setBridgeCenterInitPoint(state, { payload: offset }) {
            return { ...state, bridgeCenterOffset: offset };

        },
        setMaxY(state, { payload: max }) {
            return { ...state, maxY: max };
        },
        addRoad(state, { payload: road }) {
            return { ...state, roads: [...(state.roads && state.roads.length ? state.roads : []), road] };
        },
        addRefRoads(state, { payload: roads }) {
            return { ...state, refRoadlines: [...(state.refRoadlines && state.refRoadlines.length ? state.refRoadlines : []), ...roads] };
        },
        updateGroupRoads(state, { payload: roads }) {
            return { ...state, roads };
        },
        updateProjectRefRoads(state, { payload: refRoadlines }) {
            return { ...state, refRoadlines };
        },
        setCurrent(state, { payload: road }) {
            return { ...state, currentRoad: road };
        }
    },
    effects: {
        /**上传一组路线资源 */
        *uploadFiles({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(roadService.uploadGroupRoads, values);

            if (res && res.data && res.data.code == '200') {
                const item = res.data.data;
                yield put({
                    type: 'setCurrent',
                    payload: item
                });
                notification.info({ message: '上传线路成功' });
                onSuccess(item);

            } else {
    
                onError(res.data.message);
            }
        },
        
        *deleteRoad({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(roadService.deleteRoad, values);
            if (res && res.data && res.data.code == '200') {
                notification.info({ message: '删除线路成功' });
                onSuccess(res);
            } else {
             
                onError(res&&res.data?res.data.message:"服务器连接失败");
            }
        },
        *updateRoad({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(roadService.updateRoad, values);
            if (res && res.data && res.data.code == '200') {
                notification.info({ message: '修改线路成功' });
                onSuccess(res);
            } else {
           
                onError(res&&res.data?res.data.message:"服务器连接失败");
            }
        },
        /**更新项目所有的路线资源 */
        *loadProjectRoads({ payload: values }, { call, put }) {
            const res = yield call(roadService.queryProjectRoads, values);
            if (res.data && res.data.code == '200') {
                if (res.data.data && res.data.data.rows && res.data.data.rows.length) {
                    yield put({
                        type: 'updateGroupRoads',
                        payload: res.data.data.rows
                    });
                    return { res: res.data.data.rows, total: res.data.data.total }
                }

            } else {
                yield put({
                    type: 'updateGroupRoads',
                    payload: []
                });
                return { res: [], total: 0 }
            }

        },
        *RoadlineOriginData({ payload: values }, { call, put }) {
            const res = yield call(roadService.RoadlineOriginData, values);
            if (res && res.data && res.data.code == '200' && res.data.data) {
                return res.data.data;
            } else {
                return null
            }
        },
        
        /**查询项目所有的路线资源 */
        *queryProjectRoads({ payload: values }, { call, put }) {
            const res = yield call(roadService.queryProjectRoads, values);
            if (res && res.data && res.data.code == '200') {
                if (res.data.data && res.data.data.rows.length) {
                    for (let i = 0; i < res.data.data.rows.length; i++) {
                        const item = res.data.data.rows[i];
                        item.lines = [
                            {
                                type: 'ground',
                                name: "地面线",
                                points: null
                            }, {
                                type: 'bridge',
                                name: "桥面线",
                                points: null
                            }, {
                                type: 'bridge_center',
                                name: "中心线",
                                points: null
                            }]
                    }
                }

            }
            return res && res.data && res.data.data ? res.data.data.rows : []

        },
        /**查询项目引用的路线资源 */
        *loadRefProjectRoads({ payload: values }, { call, put, take }) {
            const res = yield call(roadService.queryProjectRefRoads, { ...values, refFlag: 0 });
            let arr = []
            if (res && res.data && res.data.code == '200' && res.data.data && res.data.data.rows.length) {
                for (let i = 0; i < res.data.data.rows.length; i++) {
                    let item = res.data.data.rows[i];
                    item.title = '道路 : ' + item.name;
                    item.key = 'group_road_' + item.id;
                    item.eventType = 'group_road_unit'
                    item.eventKey = 'group_road_unit'
                    item.checkable = false
                    arr.push({
                        ...item
                    })
                }
                yield put({
                    type: 'updateProjectRefRoads',
                    payload: arr
                });

            } else {
                yield put({
                    type: 'updateProjectRefRoads',
                    payload: []
                });
            }
            return arr
        },
        *addRefRoadsReq({ payload: { values, onSuccess, onError } }, { call, put, select }) {
            const res = yield call(roadService.addRefGroupRoad, values);
            if (res && res.data && res.data.code == '200') {
                let data = res.data.data;
                if (data && data.length) {
                    let arr = []
                    for (let i = 0; i < data.length; i++) {
                        const item = data[i];
                        if (!item.align) {
                            continue;
                        }
                        const roadMeta = item.align.Position;
                        const ref = roadMeta[0];
                        const ref1 = roadMeta[0];
                        const { code, id, name, projectId, refFlag, type } = item
                        arr.push({
                            code, id, name, projectId, refFlag, type, lines: [
                                {
                                    type: 'ground',
                                    name: "地面线",
                                    points: roadMeta.map(p => [p.PK - ref.PK, p.Z - ref.Z])
                                }, {
                                    type: 'bridge',
                                    name: "桥面线",
                                    points: roadMeta.map(p => [p.PK - ref.PK, p.Z0 - ref.Z])
                                }, {
                                    type: 'bridge_center',
                                    name: "中心线",
                                    points: roadMeta.map(p => [p.X - ref.X, p.Y - ref.Y])
                                }]
                        })
                    }
                    let refRoadlines = JSON.parse(JSON.stringify(yield select(state => state.refRoads.refRoadlines)))

                    if (refRoadlines && refRoadlines.length) {
                        for (let i = 0; i < refRoadlines.length; i++) {
                            const item = refRoadlines[i];
                            for (let j = 0; j < arr.length; j++) {
                                const item2 = arr[j];
                                if (item.id == item2.id) {
                                    if (!item.lines || !item.lines.length) {
                                        item.lines = arr[j].lines
                                    }
                                } else {
                                    if (!refRoadlines.find(obj => obj.id == item2.id)) {
                                        refRoadlines.push(item2)
                                    }
                                }
                            }
                        }
                    } else {
                        refRoadlines = res.data.data
                    }

                    yield put({
                        type: 'updateProjectRefRoads',
                        payload: refRoadlines
                    });
                    notification.info({ message: '引用线路成功' });
                }
                onSuccess(res.data.data);
            } else {
          
                onError(res.data.message);
            }
        },
        *queryRoadDetail({ payload: params }, { call, put }) {
            const res = yield call(roadService.queryRoadDetail, params);
            if (res && res.data && res.data.code == '200' && res.data.data && res.data.data.total) {
                return { res: res.data.data.rows, total: res.data.data.total }
            } else {
                return { res: [], total: 0 }
            }
        },
        *queryRoadDetail2({ payload: params }, { call, put }) {
            const res = yield call(roadService.queryRoadDetail2, params);
            if (res && res.data && res.data.code == '200' && res.data.data && res.data.data.total) {
                return { res: res.data.data.rows, total: res.data.data.total }
            } else {
                return { res: [], total: 0 }
            }
        },
        *updateMaxY({ payload: max }, { call, put }) {
            yield put({
                type: 'setMaxY',
                payload: max
            });
        },
        *updateCurrent({ payload: road }, { call, put }) {
            yield put({
                type: 'setCurrent',
                payload: road
            });
        },
        *updateOffsetPoint({ payload: point }, { call, put }) {
            yield put({
                type: 'setInitPoint',
                payload: point
            });
        }
    }
}